import {
  addExternalList,
  addExternalAdd,
  addExternalRemove,
  addExternalListItem,
  addExternalEdit,
  packageConfirmList,
  packageConfirmListItem,
  packageConfirmAdd,
  packageConfirmEdit,
  packageConfirmRemove,
  libraryReceptionList,
  libraryReceptionListItem,
  libraryReceptionAdd,
  libraryReceptionEdit,
  libraryReceptionRemove
} from '@/api/demo'
import { Message } from 'element-ui'

const getDefaultState = () => {
  return {
    addListData: [],
    packListData: [],
    libraListData: []
  }
}

const state = getDefaultState()

const mutations = {
  SET_ADD: (state, newList) => {
    state.addListData = newList
  },
  SET_PACK: (state, newList) => {
    state.packListData = newList
  },
  SET_LIBRA: (state, newList) => {
    state.libraListData = newList
  }
}

const actions = {
  addExternalList({ commit }) {
    return new Promise((resolve, reject) => {
      addExternalList({ userName: '嘉图', currentPage: 1, pageSize: 50 }).then(response => {
        const data = response.data.data.dataList
        commit('SET_ADD', data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  addExternalAdd(_, data) {
    return new Promise((resolve, reject) => {
      addExternalAdd(data).then((res) => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  addExternalRemove(_, id) {
    return new Promise((resolve, reject) => {
      addExternalRemove(id).then((res) => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  addExternalListItem(_, id) {
    return new Promise((resolve, reject) => {
      addExternalListItem(id).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  addExternalEdit(_, data) {
    return new Promise((resolve, reject) => {
      addExternalEdit(data).then((res) => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  packageConfirmList({ commit }) {
    return new Promise((resolve, reject) => {
      packageConfirmList({ userName: '嘉图', currentPage: 1, pageSize: 20 }).then(response => {
        const data = response.data.data.dataList
        commit('SET_PACK', data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  packageConfirmAdd() {
    return new Promise((resolve, reject) => {
      packageConfirmAdd({ userName: '嘉图test1', account: 'jiatutest1', bio: 'test1' }).then(() => {
        Message({
          message: '新增成功',
          type: 'success',
          center: true
        })
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  packageConfirmRemove(_, id) {
    return new Promise((resolve, reject) => {
      packageConfirmRemove(id).then(() => {
        Message({
          message: '删除成功',
          type: 'success',
          center: true
        })
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  packageConfirmListItem(_, id) {
    return new Promise((resolve, reject) => {
      packageConfirmListItem(id).then(response => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  packageConfirmEdit(_, id) {
    return new Promise((resolve, reject) => {
      packageConfirmEdit({ userName: '嘉图test1', account: 'jiatutest1', bio: 'test1', id }).then(() => {
        Message({
          message: '编辑成功',
          type: 'success',
          center: true
        })
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  libraryReceptionList({ commit }) {
    return new Promise((resolve, reject) => {
      libraryReceptionList({ userName: '嘉图', currentPage: 1, pageSize: 20 }).then(response => {
        const data = response.data.data.dataList
        commit('SET_LIBRA', data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  libraryReceptionAdd() {
    return new Promise((resolve, reject) => {
      libraryReceptionAdd({ userName: '嘉图test1', account: 'jiatutest1', bio: 'test1' }).then(() => {
        Message({
          message: '新增成功',
          type: 'success',
          center: true
        })
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  libraryReceptionRemove(_, id) {
    return new Promise((resolve, reject) => {
      libraryReceptionRemove(id).then(() => {
        Message({
          message: '删除成功',
          type: 'success',
          center: true
        })
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  libraryReceptionListItem(_, id) {
    return new Promise((resolve, reject) => {
      libraryReceptionListItem(id).then(response => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  libraryReceptionEdit(_, id) {
    return new Promise((resolve, reject) => {
      libraryReceptionEdit({ userName: '嘉图test1', account: 'jiatutest1', bio: 'test1', id }).then(() => {
        Message({
          message: '编辑成功',
          type: 'success',
          center: true
        })
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
